//
//  ResetPsdViewController.m
//  DaDaBao
//
//  Created by user on 15/8/19.
//  Copyright (c) 2015年 User. All rights reserved.
//

#import "ResetPsdViewController.h"

@interface ResetPsdViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField  * psdTextField;
@property (nonatomic,strong) UITextField  * verifiPsdTextField;
@property (nonatomic,copy)   NSString     * phoneNum;
@end

@implementation ResetPsdViewController
-(id)initWithPhoneNum:(NSString *)phoneNum
{
    if (self = [super init])
    {
        _phoneNum = phoneNum;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor hexStringToColor:@"#ebebeb"];
    
    [self createTheNavigationBar];
    [self createTheInformationView];
    
}
#pragma mark ------------创建导航栏
-(void)createTheNavigationBar
{
    [[UIApplication sharedApplication]setStatusBarHidden:FALSE];
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    topView.backgroundColor = [UIColor hexStringToColor:COLOR_VALUE];
    [self.view addSubview:topView];
    //标题
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0.0f, 20.0f, SCREEN_WIDTH, 44.0f)];
    titleLab.backgroundColor = [UIColor clearColor];
    titleLab.font = [UIFont boldSystemFontOfSize:20.0f];
    titleLab.textColor = [UIColor whiteColor];
    titleLab.text = @"忘记密码";
    titleLab.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:titleLab];
    
    //返回按钮
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0.0f, 20.0f, 64.0f, 44.0f)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(buttonBackTapped:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:backBtn];
    
}

#pragma mark--------------创建用户信息view
-(void)createTheInformationView
{
    
    UIView * userView = [[UIView alloc]initWithFrame:CGRectMake(0, 64 + 10*HSCALE, SCREEN_WIDTH , 88*HSCALE)];
    userView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:userView];
    
    UILabel * userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 40*HSCALE)];
    userNameLabel.text = @"设置新密码";
    userNameLabel.textColor = RGBA(99, 98, 99, 1);
    userNameLabel.textAlignment = NSTextAlignmentLeft;
    [userView addSubview:userNameLabel];
    
    _psdTextField = [[UITextField alloc]initWithFrame:CGRectMake(110, 0.0f, SCREEN_WIDTH - 120, 40*HSCALE)];
    _psdTextField.placeholder = @"至少6位字符";
    _psdTextField.delegate = self;
    _psdTextField.font = [UIFont systemFontOfSize:14];
    _psdTextField.secureTextEntry = TRUE;
    [userView addSubview:_psdTextField];
    
    UIView * lineView = [[UIView alloc]initWithFrame:CGRectMake(10*WSCALE, 41*HSCALE, SCREEN_WIDTH - 20*WSCALE, 1*HSCALE)];
    lineView.backgroundColor = RGBA(240, 240, 240, 1);
    [userView addSubview:lineView];
    
    UILabel * psdLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 42*HSCALE, 100, 40*HSCALE)];
    psdLabel.text = @"确认密码";
    psdLabel.textColor = RGBA(99, 98, 99, 1);
    psdLabel.textAlignment = NSTextAlignmentLeft;
    [userView addSubview:psdLabel];
    
    _verifiPsdTextField = [[UITextField alloc]initWithFrame:CGRectMake(110, 42*HSCALE, SCREEN_WIDTH - 120, 40*HSCALE)];
    _verifiPsdTextField.delegate = self;
    _verifiPsdTextField.placeholder = @"再次输入密码";
    _verifiPsdTextField.font = [UIFont systemFontOfSize:14];
    _verifiPsdTextField.secureTextEntry = TRUE;
    [userView addSubview:_verifiPsdTextField];
    
    //修改按钮
    UIButton * verifiBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 20*WSCALE, 35*HSCALE)];
    verifiBtn.center = CGPointMake(self.view.center.x, 64+142.5*HSCALE);
    verifiBtn.backgroundColor = [UIColor hexStringToColor:RED_COLOR_VALUE];
    verifiBtn.layer.cornerRadius = 5;
    verifiBtn.layer.masksToBounds = TRUE;
    verifiBtn.titleLabel.font = [UIFont boldSystemFontOfSize:19];
    [verifiBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    [verifiBtn addTarget:self action:@selector(buttonChangePsdTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:verifiBtn];
}
#pragma mark-返回方法
-(void)buttonBackTapped:(id)sender
{
    [self dismissViewControllerAnimated:TRUE completion:nil];
}
#pragma mark-----修改按钮绑定方法
-(void)buttonChangePsdTapped:(id)sender
{
    if (_psdTextField.text.length<=0)
    {
        ALERT_VIEW(@"新密码不能为空");
        return;
    }
    if (_verifiPsdTextField.text.length <=0)
    {
        ALERT_VIEW(@"确认密码不能为空");
        return;
    }
    if (![_psdTextField.text isEqualToString:_verifiPsdTextField.text]) {
        ALERT_VIEW(@"两次密码不一致");
        return;
    }
    if (_psdTextField.text.length<6)
    {
        ALERT_VIEW(@"密码长度不能少于6位");
        return;
    }
    [self.view endEditing:TRUE];//让键盘下去
    [MBProgressHUD showHUDAddedTo:self.view animated:TRUE];

    [AppGlobalClass PostDataWithNSstring:RESET_PSD withDictionary:@{@"mobile":_phoneNum,@"password":[AppGlobalClass md5:_psdTextField.text]} success:^(NSDictionary *dictionary) {
        if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
        {
            ALERT_VIEW(@"修改成功");
            //token
            [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"token"];
            [[NSUserDefaults standardUserDefaults]synchronize];
            [self.presentingViewController.presentingViewController dismissViewControllerAnimated:TRUE completion:nil];
            
        }else
        {
            NSString * str = [dictionary objectForKey:@"message"];
            ALERT_VIEW(str);
        }
        [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];

    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];

    } networkerror:^(NSString *networkerror) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];

    }];
}
#pragma mark--------textFieldDelegate
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    NSString *a = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (a.length>30) {
        
        return NO;
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
